package tcnplanner;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;

@Entity
@NamedQueries({
    @NamedQuery(name = "Reservation.getAll", query = "select u from Reservation as u"),
    @NamedQuery(name = "Reservation.findById", query = "select u from Reservation as u where u.reservationId = :id")
})
public class Reservation implements Serializable {
    public enum statusType {

        NEW, SCHEDULED, CONSTRUCTING, WAITING, INPROCESS, DONE
    };
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int reservationId; //The unique identifier to this reservation
    @Embedded
    //@AttributeOverrides({@AttributeOverride(name = "testRoom" , column = @Column(name = "Testroom"))})
    private Testroom testroom; //The room reserved for the test
    @OneToOne(cascade = CascadeType.PERSIST)
    private TestSetup testSetup; //The testsetup (has the batch of items and the price)
    @Embedded
    @Column(nullable = true)
    //@AttributeOverrides({@AttributeOverride(name = "batch" , column = @Column(name = "Batch"))})
    private Batch batch;//nodig voor de endtime//WAAROM??? - TestSetup heeft array met items? is gelijk aan batch + Batch heeft alleen een int, wat doet het?
    @Enumerated(EnumType.STRING)
    private statusType status; //word al opgeslagen in Test
    @Embedded
    private Test test; //The tests for the reservation
    @OneToOne
    private Account customer; //The customer who requested the test
    @Temporal(TemporalType.TIMESTAMP)
    private Date reservationDate; //The date the user reserved the test for (not final)    
    @Temporal(TemporalType.TIMESTAMP)
    private Date startDate; //The date the test will be executed
    //private Date endDate; //The date the test will be finished //Never used
    private String comment; //Comments on the reservation (by customer?)
    @Temporal(TemporalType.TIMESTAMP)
    private Date endDate;
    //hoelang de proef duurt in milisec
    private long duration;

    public Reservation() {
    }

    public Reservation(Date reservationDate, Testroom testroom, TestSetup testSetup, Test test, Account customer, String comment) {
        this.test = test;
        this.CalculateDuration();
        this.comment = comment;
        this.reservationDate = reservationDate;
        this.testroom = testroom;
        this.testSetup = testSetup;
        
        this.customer = customer;
        this.status = statusType.NEW;
        
        
        
    }

    public Reservation(Date reservationDate, Testroom testroom, TestSetup testSetup, Test test, Account customer) {
        this(reservationDate, testroom, testSetup, test, customer, null);
    }

    public Reservation(Date reservationDate, TestSetup testSetup, Test test, Account customer) {
        this(reservationDate, null, testSetup, test, customer, null);
    }
   
  
    public int getId() {
        return this.reservationId;
    }

    public Testroom getTestroom() {
        return this.testroom;
    }

    public void setTestroom(Testroom tr) {
        this.testroom = tr;
    }

    public TestSetup getTestSetup() {
        return this.testSetup;
    }

    public void setTestSetup(TestSetup ts) {
        this.testSetup = ts;
    }

    public Test getTest() {
        return this.test;
    }
    /*
    public void setTest(Test t) {
    this.test = t;
    }
    
    public void addTest(Test t) {
    this.test.add(t);
    }
    public void removeTest(Test t) {
    this.test.remove(t);
    }*/

    public void setStatus(statusType status) {
        this.status = status;
    }

    
    public Account getCustomer() {
        return this.customer;
    }

    /*public void setCustomer(Customer c) {
    this.customer = c;
    } //Never used*/
    public Date getReservationDate() {
        return this.reservationDate;
    }

    /*public void setReservationDate(Date d) {
    this.reservationDate = d;
    } //Never used*/
    public Date getStartDate() {
        return this.startDate;
    }

    /*
     * Sets the start date of the reservation, if this is set the reservation will be scheduled
     * @param d the scheduled start date and time
     */
    public void setStartDate() {
        this.startDate =this.reservationDate;
        this.status = statusType.SCHEDULED;
        this.setEndDate();
    }

    /* 
     * Returns the end date and time of the scheduled reservation
     * or null if the start date isnt set
     * endTime is derived from:
     * For every test i in this reservation:
     *
     * startTime + sum(Test[i]->testLengthWafer * Batch->size)
     * @return the date the reservation ends
     */

    public Date getEndDate() {  
        if(endDate ==null)this.setEndDate();
            return endDate;
        
        
    }
        

    public void setEndDate() {
        long time = this.reservationDate.getTime()+ duration;
        endDate = new Date();
        this.endDate.setTime(time);        
    }


    public boolean isScheduled() {
        if (this.status.equals(statusType.SCHEDULED)) {
            return true;
        } else {
            return false;
        }
    }

    public statusType getStatus() {
        return this.status;
    }

    public String getComment() {
        return this.comment;
    }

    public void setComment(String c) {
        this.comment = c;
    }

    public void setBatch(Batch batch) {
        this.batch = batch;
    }

    @Override
    public String toString() {
        return "" + this.reservationId;
    }
    public void setReservationDate(Date reservationDate) {
        this.reservationDate = reservationDate;
        this.setEndDate();
    }
    
    private void CalculateDuration(){   
        int count = this.getTest().getProductCount();
        //int batchsize = this.batch.getSize();
        
        long time = count * 20 * 100;
        System.out.println(time);
        int uur = 60*60*100; // uur voor opzetten en afbreken
        
        time =+ (uur*2);
        //long houres = time / uur;
        this.duration = time;
        
        
    }
}